<template>
  <div>
    <div>
      <el-input
        size="small"
        class="addPostInput"
        placeholder="添加职位..."
        suffix-icon="el-icon-plus"
        @keydown.enter.native="addPosotion"
        v-model="pos.name"
      ></el-input>
      <el-button
        size="small"
        icon="el-icon-plus"
        type="primary"
        @click="addPosotion"
        >添加</el-button
      >
    </div>
    <div class="posManaMain">
      <el-table stripe border :data="positions" style="width: 60%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"> </el-table-column>
        <el-table-column prop="id" label="编号" width="70px" align="center">
        </el-table-column>
        <el-table-column prop="name" label="职位" width="150px" align="center">
        </el-table-column>
        <el-table-column
          prop="createDate"
          label="创建时间"
          width="200px"
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column prop="enabled" label="是否启用" width="150px">
        </el-table-column> -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-button type="danger" size="small" style="margin-top: 8px" :disabled = "multipleSelection.length == 0" @click="deleteAll()">批量删除</el-button>
    <el-dialog title="修改职位" :visible.sync="dialogVisible" width="25%">
      <div>
        <el-tag>职位名称</el-tag>
        <el-input
          v-model="updatePos.name"
          size="small"
          class="updatePostInput"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdatePosition()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "posMana",
  data() {
    return {
      pos: {
        name: "",
      },
      positions: [],
      dialogVisible: false,
      updatePos: {
        name: "",
      },
      multipleSelection: [],
    };
  },
  created() {
    this.initPositions();
  },
  methods: {
    deleteAll(){
      
      this.$confirm(
        "此操作将永久删除[" + this.multipleSelection.length + "]条职位, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          var ids = '?';
          this.multipleSelection.forEach(item => {
            ids += 'ids=' +item.id + '&';
          });
          this.$deletefn("/system/basic/pos/" + ids).then((res) => {
            if (res) {
              this.initPositions();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
      // console.log(this.multipleSelection);
    },
    addPosotion() {
      if (this.pos.name) {
        this.$post("/system/basic/pos/", this.pos).then((res) => {
          if (res) {
            this.initPositions();
            this.pos.name = "";
          }
        });
      } else {
        this.$message.error("职位不能为空");
      }
    },
    handleEdit(data) {
      Object.assign(this.updatePos, data);
      this.dialogVisible = true;
    },
    handleDelete(data) {
      this.$confirm(
        "此操作将永久删除[" + data.name + "]职位, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$deletefn("/system/basic/pos/" + data.id).then((res) => {
            if (res) {
              this.initPositions();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitUpdatePosition() {
      this.$put("/system/basic/pos/", this.updatePos).then((res) => {
        if (res) {
          this.initPositions();
          this.dialogVisible = false;
        }
      });
    },
    initPositions() {
      this.$get("/system/basic/pos/").then((res) => {
        if (res) {
          this.positions = res;
        }
      });
    },
  },
};
</script>

<style scoped>
.addPostInput {
  width: 300px;
  margin-right: 8px;
}
.posManaMain {
  margin-top: 10px;
}
.updatePostInput {
  width: 200px;
  margin-left: 8px;
}
</style>